<template>
  <el-dialog title="图纸集选择"
             v-if="showFlag"
             :visible.sync="showFlag"
             :modal= false
             width="80%"
             center
  >
    <el-row :gutter="20">
      <!--图纸数据-->
      <el-col :span="24" :xs="24">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="产品图号" prop="itemDrawCode">
            <el-input
              v-model="queryParams.itemDrawCode"
              placeholder="请输入产品图号"
              clearable
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <el-table
          v-loading="loading"
          :data="drawList"
          border
          @current-change="handleCurrent"
          @row-dblclick="handleRowDbClick">
          <el-table-column width="50" align="center" >
            <template v-slot="scope">
              <el-radio v-model="selectedDrawId" :label="scope.row.id" @change="handleRowChange(scope.row)">{{""}}</el-radio>
            </template>
          </el-table-column>
          <el-table-column
              label="产品名称"
              align="center"
              key="itemName"
              prop="itemName"
              :show-overflow-tooltip="true"
          />
          <el-table-column
              label="产品图号"
              align="center"
              key="itemDrawCode"
              prop="itemDrawCode"
              :show-overflow-tooltip="true"
          />
          <el-table-column
              label="图纸分类"
              align="center"
              key="drawingTypeName"
              prop="drawingTypeName"
              :show-overflow-tooltip="true"
          />
          <el-table-column
              label="图纸集名称"
              align="center"
              key="drawingName"
              prop="drawingName"
              :show-overflow-tooltip="true"
          />
          <el-table-column
              label="图纸集版本"
              align="center"
              key="version"
              prop="version"
              :show-overflow-tooltip="true"
          />
          <el-table-column
              label="设计人"
              align="center"
              key="designer"
              prop="designer"
              :show-overflow-tooltip="true"
          />
          <el-table-column label="设计时间" align="center" prop="designDate" >
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.designDate, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
          <el-table-column label="图纸状态" align="center" prop="status">
            <template slot-scope="scope">
              <el-tag v-if="scope.row.status === 0" type="info">草稿</el-tag>
              <el-tag v-else-if="scope.row.status === 1" type="success">启用</el-tag>
              <el-tag v-else-if="scope.row.status === 2" type="danger">禁用</el-tag>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="confirmSelect">确 定</el-button>
      <el-button @click="showFlag=false">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { listDrawing } from "@/api/mes/md/drawing";

export default {
  name: "DrawingSelect",
  props: {
    itemCode: null,
  },
  data() {
    return {
      showFlag:false,
      // 选中数组
      selectedDrawId: undefined,
      selectedRows: undefined,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 图纸表格数据
      drawList: null,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        itemCode: this.itemCode,
        bomAuthorization: 1,
        status: 1,
      },
    };
  },
  created() {
    this.getList();
  },
  watch: {
    'itemCode': function (newVal, oldVal) {
      this.queryParams.itemCode = newVal;
      this.getList();
    },
  },
  methods: {
    /** 查询物料编码列表 */
    getList() {
      this.loading = true;
      listDrawing(this.queryParams).then(response => {
          this.drawList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleCurrent(row){
      if(row){
        this.selectedRows = row;
      }
    },
    // 双击事件
    handleRowDbClick(row){
      if(row){
        this.selectedRows = row;
        this.$emit('onSelected',this.selectedRows);
        this.showFlag = false;
      }
    },
    // 单选选中数据
    handleRowChange(row) {
      if(row){
        this.selectedRows = row;
      }
    },
    //确定选中
    confirmSelect(){
      if(this.selectedDrawId == null || this.selectedDrawId == 0){
        this.$notify({
          title:'提示',
          type:'warning',
          message: '请至少选择一条数据!'
        });
        return;
      }
      this.$emit('onSelected',this.selectedRows);
      this.showFlag = false;
    }
  }
};
</script>
